<?php
/**
 * Created by PhpStorm.
 * User: PGF(592472116@qq.com)
 * Date: 2016/8/22
 * Time: 10:48
 */

namespace Api\Controller;
use Think\Controller;
use Com\Message;
header("content-type:text/html;charset=utf-8");
class NotifyController extends Controller
{
	//--支付宝异步通知地址
	function index(){
        file_put_contents('./test.txt',json_encode($_POST));
		include APP_PATH.'../Alipay1/Alipay.class.php';
		//$alipay = new \Alipay();
		//$res = $alipay->verifyNotify();
        //logResult($_POST['out_trade_no']);
       // logResult($_POST['total_amount']);
        $res['trade_no']=$_POST['out_trade_no'];
        $res['total_fee']=$_POST['total_amount'];
        $order_no=$res['trade_no'];
        //logResult($order_no);


        //观看视频支付回调
        $Users = M('users','cmf_');
        $Config =  M('config_private','cmf_');
        $config = $Config->where('id=1')->find();
        $Video = M('video','cmf_');

        $Purcase = M('purchase','cmf_');
        $maps['order_no'] = $order_no;
        $maps['status'] = 0;
        $maps['money'] = rtrim(rtrim($res['total_fee'], '0'), '.');

        $pur_res=$Purcase->where($maps)->find();

        $video = $Video->where('id='.$pur_res['vid'])->find();
        if($pur_res){
            $data['status'] =1;
            $data['uptime'] = time();
            $pur_res1 = $Purcase->where($maps)->save($data);
            if($pur_res1) {
                //消费记录
                payrecord($pur_res['uid'],$pur_res['id'],$pur_res['money'],11,1);
                //系统抽成
                $watch_ratio = $pur_res['coin']*$config['watch_ratio']/100;
                $coin2= $pur_res['coin']-$watch_ratio;
                //给上传者播豆
                $Users->where('id='.$video['uid'])->setInc('votes',$coin2);
                //上传者消费记录
                payrecord($video['uid'],0,$coin2,14,2);

                echo 'SUCCESS';
            }
        }
        //推荐视频支付回调
        $Download = M('recommend_video','cmf_');
        $down_res=$Download->where($maps)->find();
        if($down_res){
              $data['status'] =1;
              $data['uptime'] = time();
              $down_res1=$Download->where($maps)->save($data);
              if($down_res1){
                  //消费记录
                  payrecord($down_res['uid'],$down_res['id'],$down_res['money'],15,1);
                  //系统抽成
                  $watch_ratio = $down_res['coin']*$config['download_ratio']/100;
                  $coin2= $down_res['coin']-$watch_ratio;

                  $Video->where('id='.$down_res['vid'])->setField('rec_level',$down_res['level']);
                  //给上传者播豆
                  $Users->where('id='.$video['uid'])->setInc('votes',$coin2);
                  //上传者消费记录
                  payrecord($video['uid'],0,$coin2,14,2);
                  echo 'SUCCESS';
              }
        }

        //打赏视频支付回调
        $Reward = M('reward','cmf_');
        $reward_res = $Reward->where($maps)->find();
        if($reward_res){
            $data['status'] = 1;
            $data['uptime'] = time();
            $reward_res1=$Reward->where($maps)->save($data);
            if($reward_res1){
                //消费记录
                payrecord($reward_res['uid'],$reward_res['id'],$reward_res['money'],13,1);
                //系统抽成
                $watch_ratio = $reward_res['coin']*$config['reward_ratio']/100;
                $coin2= $reward_res['coin']-$watch_ratio;

                //给上传者播豆
                $Users->where('id='.$video['uid'])->setInc('votes',$coin2);
                //上传者消费记录
                payrecord($video['uid'],0,$coin2,14,2);
                echo 'SUCCESS';
            }
        }

        //充值回调
        $where['amount']=$res['total_fee'];
        $where['order_no'] = $order_no;
        $where['status'] = 0;
        $re = M('recharge_order')->where($where)->find();

        if($re){
            //充值
            $map['status'] = 1;
            $map['update_time'] = NOW_TIME;
            //更新订单状态
            $r = M('recharge_order')->where('order_no='.$order_no)->save($map);
           // file_put_contents('bb.txt', json_encode($re['invite_id']));
            $data['status']=1;
            $data['uptime']=time();
            $re = M('user_trade')->where('order_id='.$order_no)->save($data);
            echo 'SUCCESS';
         }else{ //购买商品支付回调
            $pay_status=M('order')->where(array('order_no' => $order_no))->getField('pay_status');
            if($pay_status){
                echo 'SUCCESS';
                exit;
            }
            //购买商品订单回调
            $order_id = M('order')->where(array('order_no' => $order_no))->getField('id');
            if (!empty($order_id)) {
                D('order')->pay($res);
                M('order_product')->where(array('order_id' => $order_id))->save(array('create_time' => time(), 'update_time' => time()));
                $productid_result = M('order_product')->field('product_id')->where(array('order_id' => $order_id))->find();
                if (!empty($productid_result)) {
                    $userid_result = M('product')->field('uid')->where(array('id' => $productid_result['product_id']))->find();
                    if (!empty($userid_result)) {
                        $phone_result = M('ucenter_member')->field('username')->where(array('id' => $userid_result['uid']))->find();
                        $sales = M('product')->where(array('id' => $productid_result['product_id']))->getField('sales');
                        //M('product')->where(array('id' => $productid_result['product_id']))->save(array('sales' => intval($sales) + 1));
                        Message::send($phone_result['username'],'#code#='.$order_no,'45315');
                     //   Message::send($phone_result['username'], $order_no, '30411');
                    }
                }
            }
        }
	}

}